﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.ServiceModel.Syndication;
using System.Xml;
using Sob.Common;


namespace Eieboom.Models
{
    /// <summary>
    /// RSS数据操作实现
    /// </summary>
    public partial class ServiceImpl : IServices
    {
        /// <summary>
        /// 获取rss
        /// </summary>
        public List<SyndicationItem> GetRss(List<blog_varticle> list)
        {
            List<SyndicationItem> ret = new List<SyndicationItem>();

            foreach (var r in list)
            {
                var synObj = new SyndicationItem();
                synObj.Title = TextSyndicationContent.CreatePlaintextContent(r.title);
                synObj.Summary = TextSyndicationContent.CreatePlaintextContent(r.summary);
                synObj.Content = TextSyndicationContent.CreateHtmlContent(r.content);
                synObj.Links.Add(new SyndicationLink(new Uri(configinfo.Weburl+r.url)));
                synObj.PublishDate = new DateTimeOffset(r.createdate);
                synObj.LastUpdatedTime = new DateTimeOffset(r.createdate);
                ret.Add(synObj);
            }
            return ret;
        }

        /// <summary>
        /// 获取Comment RSS
        /// </summary>
        public List<SyndicationItem> GetCommentRss(List<blog_varticle> list)
        {
            List<SyndicationItem> ret = new List<SyndicationItem>();

            foreach (var r in list)
            {
                var synObj = new SyndicationItem();
                synObj.Title = TextSyndicationContent.CreatePlaintextContent("Re:"+r.title);
                synObj.Summary = TextSyndicationContent.CreatePlaintextContent(r.summary);
                synObj.Content = TextSyndicationContent.CreateHtmlContent(r.content+"<p style=\"text-align:right;\">"+r.username+" @ "+r.lastreplydate+"</p>");
                synObj.Links.Add(new SyndicationLink(new Uri(configinfo.Weburl + r.url)));
                synObj.PublishDate = new DateTimeOffset(r.createdate);
                synObj.LastUpdatedTime = new DateTimeOffset(r.createdate);
                ret.Add(synObj);
            }
            return ret;
        }


        /// <summary>
        /// 获取Tag集合
        /// </summary>
        public List<TagInfo> GetTagList(int tid, int count)
        {
            List<TagInfo> tags = new List<TagInfo>();

            IQueryable<blog_varticle> articles=GetArticles(tid, 0,0);
            string tagstr = "";
            foreach (blog_varticle article in articles)
            {
                tagstr += article.tags.Trim() + ",";
            }

            string[] arrTag = tagstr.Split(',');
            arrTag = Utils.DistinctStringArray(arrTag);

            foreach (string a in arrTag)
            {
                if (a.Trim() != "")
                {
                    tags.Add(new TagInfo { Tag = a.Trim(), Count = Utils.RegexCount(a, tagstr) });
                }
            }
            tags.Sort(delegate(TagInfo x, TagInfo y) { return y.Count - x.Count; });

            if (count > 0)
                tags = tags.Take(count).ToList();

            return tags;
        }
    }
}